package com.getpebble.android.common.model;

import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;
import com.getpebble.android.common.core.util.PblContentProviderUtil;
import com.getpebble.android.common.model.PblDataModel.Column;
import com.getpebble.android.common.model.PblDataModel.Column.Type;
import java.util.List;

public class PblFirmwareManifestModel extends PblDataModel {
    public static final Uri TABLE_URI = PblContentProviderUtil.getTableUri("manifests");

    public PblFirmwareManifestModel() throws IllegalArgumentException {
        super("manifests");
        addColumn(new Column(Type.INTEGER, "hw_platform"));
        addColumn(new Column(Type.STRING, "release_type"));
        addColumn(new Column(Type.STRING, "normal_url"));
        addColumn(new Column(Type.INTEGER, "normal_timestamp"));
        addColumn(new Column(Type.STRING, "normal_notes"));
        addColumn(new Column(Type.STRING, "normal_version"));
        addColumn(new Column(Type.STRING, "normal_SHA256"));
        addColumn(new Column(Type.STRING, "recovery_url"));
        addColumn(new Column(Type.INTEGER, "recovery_timestamp"));
        addColumn(new Column(Type.STRING, "recovery_notes"));
        addColumn(new Column(Type.STRING, "recovery_version"));
        addColumn(new Column(Type.STRING, "recovery_SHA256"));
    }

    public String getCreateTableCommand() {
        StringBuilder createCommand = new StringBuilder(super.getCreateTableCommand());
        String uniqueSpec = ", UNIQUE(hw_platform, release_type)";
        createCommand.insert(createCommand.length() - 2, uniqueSpec, 0, uniqueSpec.length());
        return createCommand.toString();
    }

    public static Cursor getFirmwareManifests(ContentResolver contentResolver, String[] projection) {
        return contentResolver.query(TABLE_URI, projection, null, null, null);
    }

    public static ContentProviderOperation getDeleteOp(List<String> ids) {
        return ContentProviderOperation.newDelete(TABLE_URI).withSelection("_id IN (" + TextUtils.join(", ", ids) + ")", null).build();
    }

    public static ContentProviderOperation getInsertOp(ContentValues contentValues) {
        return ContentProviderOperation.newInsert(TABLE_URI).withValues(contentValues).build();
    }
}
